package com.study.util;

import java.util.List;

import redis.clients.jedis.Jedis;

public class RedisString {

	public static void main(String[] args) {
		Jedis jedis = RedisUtil.getJedis();
		//设置testString的值为helloword
		jedis.set("testString", "helloword");
		//获取testString的值
		String testStringValue = jedis.get("testString");
		System.out.println("testString：" + testStringValue);
		
		//将testString的值改为hello，并返回旧值
		String oldvalue = jedis.getSet("testString", "hello");
		System.out.println("oldvalue：" + oldvalue);
		String newValue = jedis.get("testString");
		System.out.println("newValue：" + newValue);
		
		//返回字符串testString前三个字符
		String range = jedis.getrange("testString", 0, 2);
		System.out.println("range：" + range);
		
		//设置testEx的值为 this is a test 并设置过期时间为60秒
		jedis.setex("testEx", 60, "this is a test");
		String testEx = jedis.get("testEx");
		System.out.println("testEx：" + testEx);
		long liveTime = jedis.ttl("testEx");
		System.out.println("liveTime：" + liveTime);
		
		//将a的值设为av, b的值设为bv, c的值设为cv, d的值设为dv
		jedis.mset("a","av","b","bv","c","cv","d","dv");
		//获取a,b,c,d的值
		List<String> valueList = jedis.mget("a","b","c","d");
		for (String value : valueList) {
			System.out.println(value);
		}
		
		//获取字符串a的长度
		long length = jedis.strlen("a");
		System.out.println("length:" + length);
		
		//设置ai的值为10
		jedis.set("ai", "10");
		//ai增一
		jedis.incr("ai");
		//ai增5
		jedis.incrBy("ai", 5);
		//获取ai的值
		String ai = jedis.get("ai");
		System.out.println("ai:" + length);
		
		//ai减一
		jedis.decr("ai");
		//ai减6
		jedis.decrBy("ai", 6);
		//获取ai的值
		ai = jedis.get("ai");
		System.out.println("ai:" + length);
		
		//将字符串world追加到testString的值后面
		long stringLength = jedis.append("testString", "world");
		String testString = jedis.get("testString");
		System.out.println("testString:" + testString);
		
		jedis.close();
		
	}
	
}
